package com.golookon.managed;

import java.io.Serializable;
import java.text.DecimalFormat;


import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("surveyManager")
@Scope("session")
public class SurveyManager implements Serializable {
	private Integer[] scores;
	private String finalScore;
	private final int numberOfQuestions = 9;
	private String passcode;
	private String message;

	public SurveyManager() {
		scores = new Integer[numberOfQuestions];
		for (int i = 0; i < numberOfQuestions; i++) {
			scores[i] = 3;
		}
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getPasscode() {
		return passcode;
	}
	public void setPasscode(String passcode) {
		this.passcode = passcode;
	}

	public String getFinalScore() {
		return finalScore;
	}

	public void setFinalScore(String finalScore) {
		this.finalScore = finalScore;
	}

	public Integer[] getScores() {
		return scores;
	}

	public void setScores(Integer[] scores) {
		this.scores = scores;
	}

	public String goQ1() {
		return "pm:q1";
	}

	public String goQ2() {
		return "pm:q2";
	}

	public String goQ3() {
		return "pm:q3";
	}

	public String calculate() {
		float total = 0.00f;
		for (int i : scores) {
			total += i;
		}
		float f = total / numberOfQuestions;
		DecimalFormat df = new DecimalFormat("#.##");
		finalScore = df.format(f);
		System.out.println("calculated. finalScore" + finalScore);
		return "pm:result";
	}

	public String goMainView() {
		for (int i = 0; i < numberOfQuestions; i++) {
			scores[i] = 3;
		}
		return "pm:main";
	}

	public String login() {
		if (passcode.equals("Roger") || passcode.equals("John"))
			return "pm:main";
		else {
			message = "Passcode wrong, Please try again ";
			return null;
		}

	}
}
